<?php
//phpinfo();
include_once('include/top.php');
/* css	*/	

$action=fs_get_value("do");
switch($action)
{
	case	"addvideos"	     : addvideos()	         ; break;
	case	"add"	         : do_addvideos()	     ; break;
	case	"delete"	     : delete_videos()	     ; break;
	case    "searchvideos"   : searchvideos()        ; break;
	default				     : main()		         ; break;
}
?>

<script type="text/javascript">
function Check(){
    if(document.addform.title.value ==  ""){
        alert("视频标题不能为空!");
        document.addform.title.focus();
        return  false;
    }
}
</script>
<?php

function main()
{
    global $db,$teacher_id,$teacher_realname,$teacher_passport;
	$title = fs_get_value("title");
//	$costto = fs_get_value("costto");
	$from = fs_get_value("from");
	$belong = fs_get_value("belong");
//	$belonger = fs_get_value("belonger");
	$xuhao = fs_get_value("xuhao");
	$state = fs_get_value("state");
	$times = fs_get_value("times");
	$intotime = fs_get_value("intotime");
	$introduction = fs_get_value("introduction1");
	$conditionarray = array($title,$from,$belong,$xuhao,$state,$times,$intotime,$introduction);
	$valuearray = array(" and title like '%".$title."%'"," and fromer like '%".$from."%'"," and belong like '%".$belong."%'"," and xuhao = ".$xuhao.""," and state = ".$state.""," and times = ".$times.""," and intotime = ".strtotime($intotime).""," and introduction like '%".$introduction."%'");
	$sql = "select * from nettteach_videos where belonger = '".$teacher_id."'";
	for($i=0;$i<count($conditionarray);$i++ ){
		if($conditionarray[$i] == ''){
			$valuearray[$i] = '';
		}
		$sql.=$valuearray[$i];
	}
	$videos_result = $db -> query($sql);
	$recordcount = $db -> num_rows($videos_result);
	@$page=$_GET['p'];
	if ($page==null){$page=1;}
	$pagesize=15;								//每页显示数
	$pagecount=ceil($recordcount/$pagesize);	//计算总页
	if ($page>$pagecount){$page=$pagecount;}
	if ($page<1){$page=1;}
?>
<script src="./js/Main.js"></script>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="CContent">
  <tr>
    <th class="CTitle">视频管理</th>
  </tr>
  <tr>
    <td class="CPanel">
    <div class="SubMenu">
		<a href="teacher_videos.php?">视频管理</a> | 
		<a href="teacher_videos.php?do=addvideos">添加视频</a> | 
		<a href="teacher_videos.php?do=searchvideos">搜索视频</a>
	</div>
	
	<form action="?do=delete" method="post" name="fooeeform" style="margin:0px">
		<table border="0" cellpadding="0" cellspacing="0"  class="TablePanel" width="100%">
			<tr align="center" class="TDHead">
				<td nowrap><input type=button style="font-size:9pt;width:50px;" value="全选" onclick="click_allid(fooeeform,1)"/></td>
				<td nowrap><input type=button style="font-size:9pt;width:60px;" value="全取消" onclick="click_allid(fooeeform,0)"/></td>
				<td nowrap><input type="submit" style="font-size:9pt;width:50px;" value="删除" onclick="return delid()"/></td>

				<td nowrap width="500"><input type=button style="font-size:9pt;width:80px;" onclick="window.location.href('teacher_videos.php?do=searchvideos')" value="搜索视频" />
				</td>
				<td nowrap>.</td>
				<td nowrap>.</td>
				<td width="30%" nowrap></td>
				<td nowrap></td>
			</tr>
		</table>
<div id="click_idall">
    <div align="left" style="padding:5px;">
      <table border="0" cellpadding="0" cellspacing="0" class="TablePanel" align="center" width="100%">
        <tr align="center">
		  <td class="TDHead">√</td>
          <td class="TDHead" nowrap>视频标题</td>
<!--          <td class="TDHead" nowrap>所属课件</td>-->
<!--		  <td class="TDHead" nowrap>所属教师</td>
                  <td class="TDHead" nowrap>所属系/专业</td>
		  <td class="TDHead" nowrap>查看所需金币数</td>
          <td class="TDHead" nowrap>登陆账号</td>-->
		  <td class="TDHead" nowrap>视频状态</td>
          <td class="TDHead" nowrap>播放次数</td>
          <td class="TDHead" nowrap>时长</td>
          <td class="TDHead" nowrap>大小</td>
          <td class="TDHead" nowrap>入库时间</td>
          <td class="TDHead" nowrap>操 作</td>
        </tr>
<?php
	$videos_result=$db->query($sql." limit ".($page-1)*($pagesize).",$pagesize");
	while($videos_row = $db->fetch_array($videos_result))
	{
?>
		<tr id="t<?php echo($videos_row["id"]);?>" onclick="click_tr('<?php echo ($videos_row["id"]);?>')" onMouseOver="bgColor='#FF8899'" onMouseOut="bgColor='FFFFFF'" title=单击选中 align="center">
			<td height="27"><input name='id[]' type='checkbox' id="c<?php echo($videos_row["id"]);?>" value='<?php echo ($videos_row["id"]);?>' onclick="click_tr('<?php echo ($videos_row["id"]);?>')"></td>
			<td><a href="?do=addvideos&videos_id=<?php echo ($videos_row["id"]);?>"><?php echo($videos_row["title"]);?></a></td>
<!--			<td><?php echo(get_from_info('nettteach_member','realname',$videos_row['belonger']));?></td>
			<td><?php echo(fs_get_depart_major('nettteach_member',$videos_row['belonger']));?></td>
			<td><?php echo($videos_row['uploader']);?></td>-->
			<td><?php echo(fs_get_state_view($videos_row['state']));?></td>
			<td><?php echo($videos_row['times']);?></td>
			<td><?php echo($videos_row['timelong']);?></td>
            <td><?php echo($videos_row['size'])."兆";?></td>
			<td><?php echo(date("Y-m-d H:i:s",$videos_row['intotime']));?></td>
			<td><a href="?do=addvideos&videos_id=<?php echo ($videos_row["id"]);?>">编缉</a></td>
		</tr>
		<tr>
			<td height=1 colspan=10 bgcolor="#E8F1FF" onMouseOver="bgColor='#FF8899'" onMouseOut="bgColor='E7sxE7sxE7sx'"></td>
		</tr>
<?php
	}
?>
	</table>	</div>	</div>
	</form>
	
	
	

	<center>
<?php
/*分页示例:

pager(传入URL , 数据总数 , 开始条数 , 每页数目)
*/
echo(pager('?p={page}',$recordcount,($page-1)*($pagesize),$pagesize));		
?>
</center>
	</td>
	</tr>
	</table>

<?php
}
//可以查询多个表信息的函数
function get_from_info($table,$str,$id){
	global $db;
	$sql = "select * from ".$table." where id = ".$id;
	$result = $db -> query($sql);
	$row = $db -> fetch_array($result);
	return $row[$str];
}
//根据XX表 的 id 返回系名和专业名 qzr
function fs_get_depart_major($table,$i)
{
	if ( $i=='' || is_null($i) || !is_numeric($i))	{return '无';break;}
	global $db;
	$sql	=	"select d.name as departmentname,m.name as majorname from ".$table." c,nettteach_department d,nettteach_major m where c.id=$i and c.majorid = m.id and c.departmentid = d.id ";
        $result =	$db -> query($sql);
	$row	=	$db -> fetch_array($result);
	return	$row['departmentname']."/".$row['majorname'];
}
?>


<!--
编缉视频开始
-->
<?php
function addvideos()
{
    global $db,$teacher_id;
	$videos_id = fs_get_value("videos_id");
	if($videos_id != ''){
		$sql = "select * from nettteach_videos where id = ".$videos_id;
		$videos_result = $db -> query($sql);
		$videos_row = $db -> fetch_array($videos_result);
	}
?>
<script type="text/javascript">
        function add_courseware_type(){
                var html='<select name="belong[]"><option value="">请选择课件</option><?php  $sql = "select * from nettteach_courseware where state=0 and writerid = '".$teacher_id."'";$result = $db -> query($sql);while($row = $db -> fetch_array($result)){?><option value="<?php echo $row['id']?>"><?php echo($row['title'])?></option><?php }?> </select>&nbsp;&nbsp;&nbsp;&nbsp;';
                $("#question_type").append(html);
                return false;
        }
</script>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="CContent">
		<tr>
		    <th class="CTitle">视频管理</th>
		</tr>	
		<tr>
			<td class="CPanel">
    <div class="SubMenu">
		<a href="teacher_videos.php?">视频管理</a> | 
		<a href="teacher_videos.php?do=addvideos">添加视频</a> | 
		<a href="teacher_videos.php?do=searchvideos">搜索视频</a>
	</div>
			</td>
		</tr>	
		<tr>
			<td height="800" valign="top">
			<div class="middle_right_list">
			<form name="addform" method="post" action="?do=add&videos_id=<?php echo($videos_id);?>">
			<fieldset>
				<legend>添加/编缉视频</legend>
				   带<span style="color:#FF0000;">*</span>的必填。<br/><br>
                    <input type="hidden" name="lastphoto" value="<?php echo($videos_row['photo']);?>" style="width:300px;" />
                    <input type="hidden" name="lastaddress" value="<?php if($videos_id == ''){echo("/uploadfiles/videos/");}else{echo($videos_row['address']);}?>" style="width:300px;" />
                    <label><div align="right">视频标题：</div></label><input name="title" type="text" style="width:300px;" class="text" value="<?php echo($videos_row['title']);?>" /><span style="color:#FF0000;">*</span><br/></br>
					<label><div align="right">视频缩略图：</div></label><input type="hidden" name="photo" value="<?php echo($videos_row['photo']);?>" style="width:300px;" />
					<iframe frameborder="0" name="upload_frame" scrolling="no" src="teacher_upload.php" width="300px" onload="this.height=this.contentWindow.document.body.scrollHeight"></iframe><span style="color:#FF0000;">*</span><span>只能上传小于10兆的图片，格式仅限于jpg,gif,png,bmp,jpeg。</span><br/></br>
					<label><div align="right">视频地址：</div></label><input type="hidden" name="video" value="<?php if($videos_id == ''){echo("/uploadfiles/videos/");}else{echo($videos_row['address']);}?>" style="width:300px;" />
					<iframe frameborder="0" name="upload_frame_video" scrolling="no" src="teacher_upload_video.php" width="300px" style="min-height:30px;" onload="this.height=this.contentWindow.document.body.scrollHeight"></iframe><span style="color:#FF0000;">*</span><span>只能上传小于200M的视频，格式仅限于rmvb,rm,flv,avi,mpeg,mp4,3gp,wmv,mpg,mov,swf,dat,asf。</span><br/></br>
					
<!--					<label><div align="right">查看所需金币：</div></label><input name="costto" type="text" style="width:100px;" class="text" value="<?php echo($videos_row['costto']);?>" /><span style="color:#FF0000;">*</span><br/></br>-->
					<label><div align="right">视频大小：</div></label><input name="size" type="text" style="width:300px;" class="text" value="<?php echo($videos_row['size']);?>" readonly ="true"/><span style="color:#FF0000;">&nbsp;&nbsp;兆</span><br/></br>
                                        <?php if($_GET['videos_id'] != ''){?> <label><div align="right" >相关课件：</div></label>
                                        <?php if($videos_row['belong'] != '') 
                                                { 
                                                        $array = explode(',',$videos_row['belong']);
                                                        for($i=0;$i<count($array);$i++)
                                                        {
                                                                echo '<select name="belong[]"><option value="">请选择课件</option>';
                                                                echo get_videos_moreinfo('nettteach_courseware','title',$array[$i]);
                                                                echo "</select>";
                                                        }
                                                }
                                        ?>
                                        
                                        <?php }?>
                    &nbsp;&nbsp;&nbsp;&nbsp;添加相关课件:&nbsp;&nbsp;<input type="image" src="./images/add-icon.png"  onclick="return add_courseware_type()"><br><br>
                    <label></label><div id="question_type"></div><br/>
<!--					<label><div align="right">排列序号：</div></label><input name="xuhao" type="text" style="width:400px;" class="text" value="<?php echo($videos_row['xuhao']);?>"><br/></br>-->
					<label><div align="right">视频状态：</div></label>
                        <select name="state" style="width:300px;">
                            <option value="0" <?php if($videos_row['state'] == 0){echo("selected");}?>>显示</option>
                            <option value="1" <?php if($videos_row['state'] == 1){echo("selected");}?>>不显示</option>
                        </select><br/></br>
				<label><div align="right">播放次数：</div></label><input name="times" type="text" style="width:300px;" class="text" value="<?php echo($videos_row['times']);?>"><br/></br>
					<label><div align="right">视频时长：</div></label><input name="timelong" type="text" style="width:300;" class="text" value="<?php echo($videos_row['timelong']);?>"><br/></br>
                    <label><div align="right">视频简介：</div></label><textarea name="introduction1" style="width:500px;height:120px;"><?php echo($videos_row['introduction']);?></textarea><br /></br>
					<input name="intotime" type="hidden"  class="text" value="<?php if($videos_row == ''){echo(date("Y-m-d H:i:s",time()));}else{echo(date("Y-m-d H:i:s",$videos_row['intotime']));}?>">
                    <div class="submit">
					<label></label><input type="submit" value="确认提交" onclick="return Check();"/>&nbsp;&nbsp;&nbsp;&nbsp;<a class="lj" href="teacher_videos.php">返回列表</a></div>
			</fieldset>
			</form>
			</div>
			</td>
		</tr>
	</table>
<?php
}
/**
 * 移除字符串中指定的尾部字符
 *
 * @access public
 * @param str
 * @param strEnd
 * @return string
 */
function trimEnd($str, $strEnd) {
	return substr ( $str, - (strlen ( $strEnd )) ) == $strEnd ? substr ( $str, 0, strlen ( $str ) - strlen ( $strEnd ) ) : $str;
}
function do_addvideos(){
    global $db,$teacher_id,$teacher_passport;
	$title = fs_get_value("title");
//	$costto = fs_get_value("costto");
    $size = fs_get_value("size");
	$from = fs_get_value("from");
	$belong = fs_get_value("belong");
	$photo = fs_get_value("photo");
	$xuhao = fs_get_value("xuhao");
	$state = fs_get_value("state");
	$times = fs_get_value("times");
	$timelong = fs_get_value("timelong");
	$address = fs_get_value("video");
	$intotime = fs_get_value("intotime");
	$introduction = fs_get_value("introduction1");
    $lastaddress = fs_get_value("lastaddress"); //原视频地址 若换了则删除原文件
    $lastphoto = fs_get_value("lastphoto"); //原头像地址 若换了则删除原文件
        //对相关课件id进行整理
        sort($belong);
        $belong =array_unique($belong);
        for($i=0;$i<count($belong);$i++)
        {
               if($belong[$i] !='')
               {
                        $belongcourse.= $belong[$i].",";
                        
               }
        }
        
	//增加视频
	if(fs_get_value("videos_id") == ''){
		if($title != ''){
			$sql = "insert into nettteach_videos (title,uploader,belonger) value ('".$title."','".$teacher_passport."','".$teacher_id."')";
			$db -> query($sql);
			$id = mysql_insert_id();
		}
	}
	else{
		//修改视频
		$id = fs_get_value("videos_id");
		if($title != ''){
			$sql = "update nettteach_videos set title = '".$title."' where id = ".$id;
			$db -> query($sql);
		}
	}
//	if($costto != ''){
//		$sql = "update nettteach_videos set costto = ".$costto." where id = ".$id;
//		$db -> query($sql);
//	}
	if($photo != '' && $photo !=$lastphoto){
		$sql = "update nettteach_videos set photo = '".$photo."' where id = ".$id;
		$db -> query($sql);
        $file=$lastphoto; 
        if (unlink($file)) {
//           echo "The file was deleted successfully.", "n";
        } else {
//           echo "The specified file could not be deleted. Please try again.", "n";
        }
	}
	if($address != '' && $address!=$lastaddress){
		$sql = "update nettteach_videos set address = '".$address."' where id = ".$id;
		$db -> query($sql);
        $file=$lastaddress;$file = substr($file,1,strlen($lastaddress)); 
        if (unlink($file)) {
//           echo "The file was deleted successfully.", "n";
        } else {
//           echo "The specified file could not be deleted. Please try again.", "n";
        }
	}
        if($size != ''){
		$sql = "update nettteach_videos set size = '".$size."' where id = ".$id;
		$db -> query($sql);
	}
	if($from != ''){
		$sql = "update nettteach_videos set fromer = '".$from."' where id = ".$id;
		$db -> query($sql);
	}
	if(trimEnd($belongcourse, ',') != ''){
		$sql = "update nettteach_videos set belong = '".trimEnd($belongcourse, ',')."' where id = ".$id;
		$db -> query($sql);
	}
	if($xuhao != ''){
		$sql = "update nettteach_videos set xuhao = ".$xuhao." where id = ".$id;
		$db -> query($sql);
	}
	if($state != ''){
		$sql = "update nettteach_videos set state = ".$state." where id = ".$id;
		$db -> query($sql);
	}
	else{
		$sql = "update nettteach_videos set state = null where id = ".$id;
		$db -> query($sql);
	}
	if($times != ''){
		$sql = "update nettteach_videos set times = ".$times." where id = ".$id;
		$db -> query($sql);
	}
	if($timelong != ''){
		$sql = "update nettteach_videos set timelong = '".$timelong."' where id = ".$id;
		$db -> query($sql);
	}
	if($intotime == ''){
		$sql = "update nettteach_videos set intotime = 0 where id = ".$id;
		$db -> query($sql);
	}
	else{
		$sql = "update nettteach_videos set intotime = ".strtotime($intotime)." where id = ".$id;
		$db -> query($sql);
	}
	if($introduction != ''){
		$sql = "update nettteach_videos set introduction = '".$introduction."' where id = ".$id;
		$db -> query($sql);
	}
	if(fs_get_value("videos_id") == ''){
		fs_showmsg("添加成功！");
	}
	else{
		fs_showmsg("修改成功！");
	}
}
?>
<!--
编缉视频结束
-->

<?php
function searchvideos()
{
?>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="CContent">
		<tr>
		    <th class="CTitle">视频管理</th>
		</tr>	
		<tr>
			<td class="CPanel">
    <div class="SubMenu">
		<a href="teacher_videos.php?">视频管理</a> | 
		<a href="teacher_videos.php?do=addvideos">添加视频</a> | 
		<a href="teacher_videos.php?do=searchvideos">搜索视频</a>
	</div>
			</td>
		</tr>	
		<tr>
			<td>
			<div class="middle_right_list">
			<form name="searchform" method="post" action="?">
			<fieldset>
				<legend>搜索视频</legend><br><br>
					<label><div align="right">视频标题：</div></label><input name="title" type="text" style="width:300px;" class="text" value="" /><br/><br/>
<!--					<label><div align="right">所需金币数：</div></label><input name="costto" type="text" style="width:100px;" class="text" value="" /><br/>-->
<!--					<label><div align="right">视频来源：</div></label><input name="from" type="text" style="width:400px;" class="text" value=""><br/><br/>-->
					<label><div align="right">相关课件：</div></label>
					<select name="belong" style="width:300px">
					    <option value="">请选择课件</option>
						<?php echo(get_videos_moreinfo('nettteach_courseware','title',null));?>
					</select><br/><br/>
<!--					<label><div align="right">排列序号：</div></label><input name="xuhao" type="text" style="width:400px;" class="text" value=""><br/><br/>-->
					<label><div align="right">视频状态：</div></label>
					<select name="state" style="width:300px;">
					    <option value="">请选择状态</option>
					    <option value="1">不显示</option>
					    <option value="0">显示</option>
					</select><br/><br/>
					<label><div align="right">播放次数：</div></label><input name="times" type="text" style="width:300px;" class="text" value=""><br/><br/>
					<label><div align="right">入库时间：</div></label><input name="intotime" type="text" style="width:300px;" class="text" value=""><br/><br/>
					<label><div aling="right">视频简介：</div></label><textarea name="introduction1" style="width:500px;height:200px;"></textarea><br /><br/>
					<div class="submit">
					<label></label><input type="submit" value="确认搜索"/>&nbsp;&nbsp;&nbsp;&nbsp;<a class="lj" href="teacher_videos.php">返回列表</a></div>
			</fieldset>
			</form>
			</div>
			</td>
		</tr>
	</table>
<?php
}
//当前课件下拉列表
function get_videos_moreinfo($table,$field,$i){
	global $db,$teacher_id;
	$sql = "select * from ".$table." where writerid = '".$teacher_id."' and state=0";
	$result = $db -> query($sql);
	while($row = $db -> fetch_array($result)){
		if($row['id'] == $i){
			$str.="<option value='".$row['id']."' selected>".$row[$field]."</option>";
		}
		else{
			$str.="<option value='".$row['id']."'>".$row[$field]."</option>";
		}
	}
	return $str;return false;
}
?>

<?php
//删除视频
function delete_videos()
{
    global $db;
	$i=0;
	$videos_id = $_POST['id'];
	while($videos_id){
	    if($videos_id[$i]==null) break;
        $sql = "select * from nettteach_videos where id = ".$videos_id[$i]."";
        $result =$db -> query($sql);
        $row=$db ->fetch_array($result);
        $file=$row['address'];
        $file = substr($file,1,strlen($row['address'])); 
        
        if (unlink($file)) {
//           echo "The file was deleted successfully.", "n";
        } else {
//           echo "The specified file could not be deleted. Please try again.", "n";
        }
        $file_photo = $row['photo']; 
        if (unlink($file_photo)) {
//           echo "The file was deleted successfully.", "n";
        } else {
//           echo "The specified file could not be deleted. Please try again.", "n";
        }
		$sql = "delete from nettteach_videos where id = ".$videos_id[$i]."";
	    $db -> query($sql);
	    $i++;
	}
	fs_showmsg('删除成功!');
}
?>
